package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.OrderDetails;
import com.ruoyi.system.mapper.OrderDetailsMapper;
import com.ruoyi.system.service.IOrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单详情Service业务层处理
 *
 * @author 王家宁
 * @date 2024-09-02
 */
@Service
public class OrderDetailsServiceImpl extends ServiceImpl<OrderDetailsMapper, OrderDetails> implements IOrderDetailsService {
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    /**
     * 查询订单详情
     *
     * @param id 订单详情主键
     * @return 订单详情
     */
    @Override
    public OrderDetails selectOrderDetailsById(Long id) {
        return orderDetailsMapper.selectOrderDetailsById(id);
    }

    /**
     * 查询订单详情列表
     *
     * @param orderDetails 订单详情
     * @return 订单详情
     */
    @Override
    public List<OrderDetails> selectOrderDetailsList(OrderDetails orderDetails) {
        return orderDetailsMapper.selectOrderDetailsList(orderDetails);
    }

    /**
     * 新增订单详情
     *
     * @param orderDetails 订单详情
     * @return 结果
     */
    @Override
    public int insertOrderDetails(OrderDetails orderDetails) {
        orderDetails.setCreateTime(DateUtils.getNowDate());
        orderDetails.setUpdateTime(DateUtils.getNowDate());
        return orderDetailsMapper.insertOrderDetails(orderDetails);
    }

    /**
     * 修改订单详情
     *
     * @param orderDetails 订单详情
     * @return 结果
     */
    @Override
    public int updateOrderDetails(OrderDetails orderDetails) {
        orderDetails.setUpdateTime(DateUtils.getNowDate());
        return orderDetailsMapper.updateOrderDetails(orderDetails);
    }

    /**
     * 批量删除订单详情
     *
     * @param ids 需要删除的订单详情主键
     * @return 结果
     */
    @Override
    public int deleteOrderDetailsByIds(String ids) {
        return orderDetailsMapper.deleteOrderDetailsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单详情信息
     *
     * @param id 订单详情主键
     * @return 结果
     */
    @Override
    public int deleteOrderDetailsById(Long id) {
        return orderDetailsMapper.deleteOrderDetailsById(id);
    }

    /**
     * 根据orderId查询订单详情
     * @param id
     * @return
     */
    @Override
    public OrderDetails selectOrderDetailsByOrderId(Long id) {
        return orderDetailsMapper.selectOne(
                new LambdaQueryWrapper<OrderDetails>()
                        .eq(OrderDetails::getOrderId, id)
        );
    }

    @Override
    public List<OrderDetails> selectOrderDetailListByOrderId(Long id) {
        return orderDetailsMapper.selectList(
                new LambdaQueryWrapper<OrderDetails>()
                        .select(OrderDetails::getOrderId,OrderDetails::getQuantity,OrderDetails::getSetmealId,OrderDetails::getProductId,OrderDetails::getCreateTime)
                        .eq(OrderDetails::getOrderId, id)
        );
    }
}
